/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.AliasBO;
import org.ipti.br.tag.bo.AsksBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.QuestResultBO;
import org.ipti.br.tag.bo.QuestionsBO;
import org.ipti.br.tag.bo.QuestionsPersonBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Asks;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.QuestResult;
import org.ipti.br.tag.entity.Unity;
import java.util.ArrayList;
import java.util.Calendar;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.util.List;
import javax.faces.model.SelectItem;

/**
 *
 * @author ipti
 */
@ManagedBean(name = "questionnaires")
@ViewScoped
public class QuestionnairesBean extends AppBaseBean {

    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    @Inject
    private AsksBO asksBO;
    @Inject
    private AliasBO aliasBO;
    @Inject
    private QuestResultBO questResultBO;
    @Inject
    private QuestionsBO questionsBO;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private ActorBO actorBO;
    @Inject
    private QuestionsPersonBO questionsPersonBO;
    private QuestResult questResult;
    private Long actorID;
    private Long organizationID;
    private Long orgFatherID;
    private Long personageID;
    private Long questionID;
    private Long unityFatherID;
    private Long unityID;
    private String questionnaireName;
    private boolean success;
    private boolean requiredAgreement;
    private Calendar measureDate;
    private List<Actor> actorList;
    private List<Asks> asksList;
    private List<SelectItem> organizationList;
    private List<SelectItem> unityList;

    public QuestionnairesBean() {
    }

    public void initQuestionnaires() {
        questionnaireName = getQuestionsBO().find(questionID).getName();
        measureDate = Calendar.getInstance();
        asksList = getAsksBO().asksPerQuestion(questionID);
        requiredAgreement = getQuestionsBO().find(questionID).isAgreementRequired();
    }

    public void generateQuestionnairesList() {
        actorList = new ArrayList<Actor>();
        if (unityID != 0) {
            actorList = getActorBO().actorsPerUnityPersonage(unityID, personageID);
            if (requiredAgreement) {
                List<Actor> actorAuxList = new ArrayList<Actor>();
                for (Actor act : actorList) {
                    if (questionsPersonBO.existsQuestionsPerson(questionID, act.getPerson().getId())) {
                        actorAuxList.add(act);
                    }
                }
                actorList = actorAuxList;
            }
            for (Actor act : actorList) {
                List<QuestResult> questResultList = new ArrayList<QuestResult>();
                for (Asks ask : asksList) {
                    QuestResult qr = new QuestResult();
                    qr.setAsk(ask);
                    qr.setPerson(act.getPerson());
                    questResultList.add(qr);
                }
                act.getPerson().setQuestResultList(questResultList);
            }
        }
    }

    public void save() {
        if (!getQuestResultBO().dateAlreadyExists(measureDate, questionID)) {
            for (Actor act : actorList) {
                for (QuestResult qr : act.getPerson().getQuestResultList()) {
                    qr.setData(measureDate);
                    questResultBO.create(qr);
                }
            }
        } else {
            for (Actor act : actorList) {
                for (Asks ask : asksList) {
                    QuestResult qrOld = getQuestResultBO().findQuestResultPerPersonAskDate(act.getPerson().getId(), ask.getId(), measureDate);
                    for (QuestResult qr : act.getPerson().getQuestResultList()) {
                        String value = qr.getValue();
                        qr = qrOld;
                        qr.setValue(value);
                        questResultBO.edit(qr);
                    }
                }
            }
        }

        success = true;
    }

    public void redirectNewSearch() {
        redirect("/educational/questions/insertedQuestionnaires.xhtml?organization=" + orgFatherID + "&personage=" + personageID + "&question=" + questionID);
        success = false;
    }

    public void loadOrganizationList() {
        organizationID = 0L;
        unityID = 0L;
        organizationList = new ArrayList<SelectItem>();
        unityList = new ArrayList<SelectItem>();
        actorList = new ArrayList<Actor>();
        Organization organization = getOrganizationBO().find(orgFatherID);
        organizationList.add(new SelectItem(0L, "Selecione uma Organização"));
        if (!getOrganizationBO().getOrganizationChildren(organization.getId()).isEmpty()) {
            getAllChildren(organization);
        }
    }


    public void loadUnityList() {
        unityID = 0L;
        unityList = new ArrayList<SelectItem>();
        actorList = new ArrayList<Actor>();
        if (organizationID != 0) {
            unityFatherID = getSessionBean().getLoggedActor().getUnity().getId();
            unityList.add(new SelectItem(0L, "Selecione uma Unidade"));
            for (Unity u : getUnityBO().getUnitiesPerOrganizationUnityFatherYear(organizationID, unityFatherID, Calendar.getInstance().get(Calendar.YEAR))) {
                unityList.add(new SelectItem(u.getId(), u.getName()));
            }
        }
    }

    public void getAllChildren(Organization organization) {
        List<Organization> organizationAux = getOrganizationBO().getOrganizationChildren(organization.getId());
        for (Organization org : organizationAux) {
            organizationList.add(new SelectItem(org.getId(), org.getName()));
        }
    }

    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public Long getActorID() {
        return actorID;
    }

    public void setActorID(Long actorID) {
        this.actorID = actorID;
    }

    public AliasBO getAliasBO() {
        return aliasBO;
    }

    public void setAliasBO(AliasBO aliasBO) {
        this.aliasBO = aliasBO;
    }

    public AsksBO getAsksBO() {
        return asksBO;
    }

    public void setAsksBO(AsksBO asksBO) {
        this.asksBO = asksBO;
    }

    public Long getOrgFatherID() {
        return orgFatherID;
    }

    public void setOrgFatherID(Long orgFatherID) {
        this.orgFatherID = orgFatherID;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public Long getOrganizationID() {
        return organizationID;
    }

    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }

    public QuestResultBO getQuestResultBO() {
        return questResultBO;
    }

    public void setQuestResultBO(QuestResultBO questResultBO) {
        this.questResultBO = questResultBO;
    }

    public QuestionsBO getQuestionsBO() {
        return questionsBO;
    }

    public void setQuestionsBO(QuestionsBO questionsBO) {
        this.questionsBO = questionsBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public Long getUnityFatherID() {
        return unityFatherID;
    }

    public void setUnityFatherID(Long unityFatherID) {
        this.unityFatherID = unityFatherID;
    }

    public Long getUnityID() {
        return unityID;
    }

    public void setUnityID(Long unityID) {
        this.unityID = unityID;
    }

    public List<SelectItem> getUnityList() {
        if (unityList == null) {
            unityList = new ArrayList<SelectItem>();
        }
        return unityList;
    }

    public void setUnityList(List<SelectItem> unityList) {
        this.unityList = unityList;
    }

    public List<Actor> getActorList() {
        if (actorList == null) {
            actorList = new ArrayList<Actor>();
        }
        return actorList;
    }

    public void setActorList(List<Actor> actorList) {
        this.actorList = actorList;
    }

    public List<SelectItem> getOrganizationList() {
        if (organizationList == null) {
            loadOrganizationList();
        }
        return organizationList;
    }

    public void setOrganizationList(List<SelectItem> organizationList) {
        this.organizationList = organizationList;
    }

    public QuestResult getQuestResult() {
        return questResult;
    }

    public void setQuestResult(QuestResult questResult) {
        this.questResult = questResult;
    }

    public Long getQuestionID() {
        return questionID;
    }

    public void setQuestionID(Long questionID) {
        this.questionID = questionID;
    }

    public List<Asks> getAsksList() {
        if (asksList == null) {
            asksList = new ArrayList<Asks>();
        }
        return asksList;
    }

    public void setAsksList(List<Asks> asksList) {
        this.asksList = asksList;
    }

    public Calendar getMeasureDate() {
        return measureDate;
    }

    public void setMeasureDate(Calendar measureDate) {
        this.measureDate = measureDate;
    }

    public Long getPersonageID() {
        return personageID;
    }

    public void setPersonageID(Long personageID) {
        this.personageID = personageID;
    }

    public String getQuestionnaireName() {
        return questionnaireName;
    }

    public void setQuestionnaireName(String questionnaireName) {
        this.questionnaireName = questionnaireName;
    }

    public QuestionsPersonBO getQuestionsPersonBO() {
        return questionsPersonBO;
    }

    public void setQuestionsPersonBO(QuestionsPersonBO questionsPersonBO) {
        this.questionsPersonBO = questionsPersonBO;
    }

    public boolean isRequiredAgreement() {
        return requiredAgreement;
    }

    public void setRequiredAgreement(boolean requiredAgreement) {
        this.requiredAgreement = requiredAgreement;
    }
}
